<?php
/**
 * 京东房源同步通过_id
 */

namespace app\crontab\controller\once;


use app\crontab\model\House;
use custom\Log;
use custom\Mongo;
use custom\Redis;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;
use think\Exception;

class HouseJd extends Command {

    public static $tableName = 'house_jd_log';
    public static $sourceType = 1;
    public static $redisKey = 'house_jd_log';

    protected function configure() {
        $this->setName('HouseJd');
    }

    protected function execute(Input $input, Output $output) {
        $this->initial();
    }

    public function initial() {
        echo "starting\n";
        $mongo = Mongo::getInstance();
        $redisModel = Redis::getInstance();
        $suc = 0;
        $total = 0;
        $wait = 0;//等待次数
        $errNum = 0;//错误次数
        while (true) {
            try {
                $_id = $redisModel->get(self::$redisKey);
                if ($_id === false) break;
                //历史id+1
                $_id = (int)$_id + 1;
                $cursor = $mongo->select(self::$tableName, ['_id' => $_id], ['limit' => 1]);
                if (empty($cursor)) {
                    if ($wait >= 3) break;
                    echo "waiting...\n";
                    sleep(600);
                    $wait++;
                    continue;
                };
                $data = (array)$cursor[0];
                $total++;
                //验证参数
                House::checkParams($data, ['house_id', 'title', '_id']);
                //删除为空的字段
                foreach ($data as $key => $datum) if (empty($datum)) unset($data[$key]);
                //数据处理
                $has_house_auction = db("house_auction")->where("source_id", $data['house_id'])->find();
                $has_house = db("house")->where('sn', md5($data['title']))->find();
                Db::startTrans();
                //更新拍卖、房源
                if ($has_house_auction && $has_house) {
                    House::handleSyncData($data, $has_house_auction, $has_house);
                } //已存在房源绑定新的拍卖信息
                elseif (empty($has_house_auction) && $has_house) {
                    House::bindNewAuction($data, $has_house, self::$sourceType);
                } //新增拍卖&房源数据
                else {
                    House::addNewData($data, self::$sourceType);
                }
                //处理完毕
                Db::commit();
                $redisModel->set(self::$redisKey, $data['_id']);
                $suc++;
            } catch (Exception $e) {
                Db::rollback();
                Db::close();
                $err = "id:{$_id}|" . $e->getMessage() . ' (' . $e->getCode() . '), ' . $e->getFile() . ' (' . $e->getLine() . ')' . "\n" . $e->getTraceAsString() . "\r\n\r\n";
                if ($errNum >= 3) break;
                $errNum++;
                //跳过这条错误房源
                $redisModel->set(self::$redisKey, $data['_id'] + 1);
                Log::write($err, "JdhouseErr.log");
                continue;
            }
        }
        echo 'total->' . $total . PHP_EOL;
        echo 'suc->' . $suc . PHP_EOL;
        echo "end" . PHP_EOL;
        Log::write("total->$total;suc->$suc", "JdhouseResult.log");
    }


}
